package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.JiajiaMallOrder;
import com.ruoyi.system.domain.JiajiaPointsFlowUserd;
import com.ruoyi.system.mapper.JiajiaPointsFlowUserdMapper;
import com.ruoyi.system.service.IJiajiaPointsFlowUserdService;
import com.ruoyi.system.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 已用积分流水Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-04-16
 */
@Service
public class JiajiaPointsFlowUserdServiceImpl implements IJiajiaPointsFlowUserdService 
{
    @Autowired
    private JiajiaPointsFlowUserdMapper jiajiaPointsFlowUserdMapper;

    /**
     * 查询已用积分流水
     * 
     * @param id 已用积分流水主键
     * @return 已用积分流水
     */
    @Override
    public JiajiaPointsFlowUserd selectJiajiaPointsFlowUserdById(Long id)
    {
        return jiajiaPointsFlowUserdMapper.selectJiajiaPointsFlowUserdById(id);
    }

    /**
     * 查询已用积分流水列表
     * 
     * @param jiajiaPointsFlowUserd 已用积分流水
     * @return 已用积分流水
     */
    @Override
    public List<JiajiaPointsFlowUserd> selectJiajiaPointsFlowUserdList(JiajiaPointsFlowUserd jiajiaPointsFlowUserd)
    {
        return jiajiaPointsFlowUserdMapper.selectJiajiaPointsFlowUserdList(jiajiaPointsFlowUserd);
    }

    /**
     * 新增已用积分流水
     * 
     * @param jiajiaPointsFlowUserd 已用积分流水
     * @return 结果
     */
    @Override
    public int insertJiajiaPointsFlowUserd(JiajiaPointsFlowUserd jiajiaPointsFlowUserd)
    {
        jiajiaPointsFlowUserd.setCreateTime(DateUtils.getNowDate());
        return jiajiaPointsFlowUserdMapper.insertJiajiaPointsFlowUserd(jiajiaPointsFlowUserd);
    }

    @Override
    public void saveJiajiaPointsFlowUserds(JiajiaMallOrder jiajiaMallOrder) {
        //已用积分入库
        BigDecimal profitPoints = jiajiaMallOrder.getProfitPoints();
        BigDecimal freePoints = jiajiaMallOrder.getFreePoints();
        BigDecimal queuePoints = jiajiaMallOrder.getQueuePoints();
        if(profitPoints.compareTo(new BigDecimal(0))>0){
            JiajiaPointsFlowUserd jiajiaPointsFlowUserd=new JiajiaPointsFlowUserd();
            jiajiaPointsFlowUserd.setUid(TokenUtil.getUid());
            jiajiaPointsFlowUserd.setUserPoints(profitPoints);
            jiajiaPointsFlowUserd.setPointsType("1");
            jiajiaPointsFlowUserd.setUsedOrderId(jiajiaMallOrder.getId());
            jiajiaPointsFlowUserdMapper.insertJiajiaPointsFlowUserd(jiajiaPointsFlowUserd);
        }
        if(freePoints.compareTo(new BigDecimal(0))>0){
            JiajiaPointsFlowUserd jiajiaPointsFlowUserd=new JiajiaPointsFlowUserd();
            jiajiaPointsFlowUserd.setUid(TokenUtil.getUid());
            jiajiaPointsFlowUserd.setUserPoints(freePoints);
            jiajiaPointsFlowUserd.setPointsType("0");
            jiajiaPointsFlowUserd.setUsedOrderId(jiajiaMallOrder.getId());
            jiajiaPointsFlowUserdMapper.insertJiajiaPointsFlowUserd(jiajiaPointsFlowUserd);
        }
        if(queuePoints.compareTo(new BigDecimal(0))>0){
            JiajiaPointsFlowUserd jiajiaPointsFlowUserd=new JiajiaPointsFlowUserd();
            jiajiaPointsFlowUserd.setUid(TokenUtil.getUid());
            jiajiaPointsFlowUserd.setUserPoints(queuePoints);
            jiajiaPointsFlowUserd.setPointsType("2");
            jiajiaPointsFlowUserd.setUsedOrderId(jiajiaMallOrder.getId());
            jiajiaPointsFlowUserdMapper.insertJiajiaPointsFlowUserd(jiajiaPointsFlowUserd);
        }
    }

    /**
     * 修改已用积分流水
     * 
     * @param jiajiaPointsFlowUserd 已用积分流水
     * @return 结果
     */
    @Override
    public int updateJiajiaPointsFlowUserd(JiajiaPointsFlowUserd jiajiaPointsFlowUserd)
    {
        jiajiaPointsFlowUserd.setUpdateTime(DateUtils.getNowDate());
        return jiajiaPointsFlowUserdMapper.updateJiajiaPointsFlowUserd(jiajiaPointsFlowUserd);
    }

    /**
     * 批量删除已用积分流水
     * 
     * @param ids 需要删除的已用积分流水主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaPointsFlowUserdByIds(String ids)
    {
        return jiajiaPointsFlowUserdMapper.deleteJiajiaPointsFlowUserdByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除已用积分流水信息
     * 
     * @param id 已用积分流水主键
     * @return 结果
     */
    @Override
    public int deleteJiajiaPointsFlowUserdById(Long id)
    {
        return jiajiaPointsFlowUserdMapper.deleteJiajiaPointsFlowUserdById(id);
    }
}
